package com.uphone.kingmall.app;

import android.app.Application;
import android.content.Context;
import android.util.DisplayMetrics;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.lljjcoder.style.citylist.utils.CityListLoader;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.Tencent;
import com.uphone.kingmall.utils.ConstansUtils;

import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/15
 * desc   : xxxx 描述
 */
public class MyApplication {
    public static Application mApplication;
    public static Context mContext;
    public static int width, height;

    public static IWXAPI api;
    public static Tencent qqTencent;

    public void onCreate(Application application) {
        mContext = application;
        mApplication = application;
        DisplayMetrics metrics = application.getResources().getDisplayMetrics();
        width = metrics.widthPixels;
        height = metrics.heightPixels;

        initOKgo();
        initLog();
        initWechat();
        initQQ();
/**
 * 预先加载三级列表显示省市区的数据
 */
        CityListLoader.getInstance().loadProData(application);
        // 融云
//        RongIM.setServerInfo("nav.cn.ronghub.com", "up.qbox.me");
//        RongIM.init(this, "25wehl3u2gg9w");
        //极光
//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);
        getId();

    }

    public String getId() {
//        String registrationID = JPushInterface.getRegistrationID(this);
//        return registrationID;
        return "";
    }

    // 下拉加载 上拉刷新的设置
//    //static 代码段可以防止内存泄露
//    static {
//        //设置全局的Header构建器
//        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
//            @Override
//            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
//                layout.setPrimaryColorsId(R.color.colorBg, R.color.black);//全局设置主题颜色
//                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
//            }
//        });
//        //设置全局的Footer构建器
//        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
//            @Override
//            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//                //指定为经典Footer，默认是 BallPulseFooter
//                layout.setPrimaryColorsId(R.color.colorBg, R.color.black);//全局设置主题颜色
//                return new ClassicsFooter(context);
//            }
//        });
//        //代码设置刷新加载样式
//        //设置 Header 为 贝塞尔雷达 样式
////        refreshLayout.setRefreshHeader(new BezierRadarHeader(getActivitySafely()).setEnableHorizontalDrag(true));
//        //设置 Footer 为 球脉冲 样式
////        refreshLayout.setRefreshFooter(new BallPulseFooter(getActivitySafely()).setSpinnerStyle(SpinnerStyle.Scale));
//    }

    /**
     * 注册微信
     */
    private void initWechat() {
        api = WXAPIFactory.createWXAPI(mContext, ConstansUtils.WECHAT_APPID);
        boolean b = api.registerApp(ConstansUtils.WECHAT_APPID);
        LogUtils.i("注册微信结果=" + b);
    }

    /**
     * 注册QQ
     */
    private void initQQ() {
        qqTencent = Tencent.createInstance(ConstansUtils.QQ_APPID, mContext);
    }

    /**
     * 初始化自定义log
     */
    private void initLog() {
        LogUtils.Config config = LogUtils.getConfig();
//        config.setLogSwitch(false);//log开关
        config.setGlobalTag(AppUtils.getAppName());//全局tag
        config.setLogHeadSwitch(true);//头部信息开关
        config.setBorderSwitch(true);//边框开关
        config.setSingleTagSwitch(true);//单一tag开关
    }

    /**
     * 初始化okgo的log
     */
    private void initOKgo() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //初始化OKGo,网络框架
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(AppUtils.getAppName());
        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        //log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);
        OkGo.getInstance().init(mApplication).setOkHttpClient(builder.build());
    }
}
